package com.computercourse.order.dao;

import com.computercourse.framework.domain.task.CicTask;
import com.github.pagehelper.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;

@Repository
public interface CicTaskRepository extends JpaRepository<CicTask, String> {

    //取出指定时间之前的记录
    Page<CicTask> findByUpdateTimeBefore(Pageable pageable, Date updateTime);

    //更新updateTime
    @Modifying
    @Query("update CicTask t set t.updateTime = :updateTime where id = :id")
    int updateTaskTime(@Param("id") String id, @Param("updateTime") Date updateTime);

    //使用乐观锁方式校验任务id和版本号是否匹配，匹配则版本号加1
    @Modifying
    @Query("update CicTask t set t.version = :version+1 where t.id = :id and t.version = :version")
    int updateTaskVersion(@Param(value = "id") String id, @Param(value = "version") int version);

}